package com.dwjf.core.net;

import android.content.Context;

import com.dwjf.core.net.callbak.Failure;
import com.dwjf.core.net.callbak.Request;
import com.dwjf.core.net.callbak.RequestCallBacks;
import com.dwjf.core.net.callbak.Success;
import com.dwjf.core.net.callbak.Error;
import com.dwjf.core.ui.LatteLoader;
import com.dwjf.core.ui.LoaderStyle;


import java.util.Map;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;

public class RestClient {

    private final String url;
    private final Map<String, Object> params;
    private final Request request;
    private final Success success;
    private final Error error;
    private final Failure failure;
    private final RequestBody body;
    private final LoaderStyle loaderStyle;
    private final Context context;

    public RestClient(String url, Map<String, Object> params,
                      Request request, Success success, Error error,
                      Failure failure, RequestBody body,
                      LoaderStyle loaderStyle, Context context){
        this.url = url;
        this.params = params;
        this.request = request;
        this.success = success;
        this.error = error;
        this.failure = failure;
        this.body = body;
        this.loaderStyle = loaderStyle;
        this.context = context;
    }


    public static RestClientBulider build(){
        return new RestClientBulider();
    }


    public void request(HttpMethod method){
        RestService service = RestCreator.getInstance().getRestService();
        Call<String> call = null;
        if (request != null){
            request.onRequestStart();
        }

        if (loaderStyle != null){
            LatteLoader.showLoding(context, loaderStyle);
        }
        switch (method){
            case GET:
                call = service.get(url, params);
                break;
            case POST:
                call = service.post(url, params);
                break;
            case PUT:
                call = service.put(url, params);
                break;
            case DELETE:
                call = service.delete(url,params);
                break;
            case UPLOAD:
                break;
            case DOWLOAD:
                break;
            default:
                break;
        }

        if (call != null){
            call.enqueue(getRequestCallback());
        }
    }

    private Callback<String> getRequestCallback(){
        return new RequestCallBacks(
                request,
                success,
                error,
                failure,
                loaderStyle
        );
    }

    public final void  get(){
        request(HttpMethod.GET);
    }

    public final void  post(){
        request(HttpMethod.POST);
    }

    public final void  put(){
        request(HttpMethod.PUT);
    }

    public final void  delete(){
        request(HttpMethod.DELETE);
    }

}
